package com.yxb.text.filter;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanda.controller.Wd_FunctionController;
import com.wanda.model.User_functionModel;
import com.wanda.model.Wd_FunctionModel;
import com.wanda.model.Wd_UserModel;
import com.wanda.service.Wd_RoleService;
import com.wanda.service.impl.Wd_FunctionServiceImpl;

public class TestFilter extends org.springframework.web.filter.CharacterEncodingFilter{

	 
	
	
	@Override
	protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {
	 

		String str =request.getRequestURI().toString(); 
		Wd_UserModel userModel   =(Wd_UserModel) request.getSession().getAttribute("bean");
	    //System.out.println(str);
		
		
		List<String> functions =(List<String>) request.getSession().getAttribute("functions");
		List<String> function=(List<String>) request.getSession().getAttribute("function");
		if(functions!=null){
	//	System.out.println(functions.size());
		}
		if(function!=null){
	//	System.out.println(function.size());
			}
		
		
		if(functions!=null){
        if(functions.contains(str)){
        	if(function.contains(str)){  
        		//绑定并且通过的事件
        		super.doFilterInternal(request, response, filterChain);	
        	}else{
        		  request.setCharacterEncoding("UTF-8");
          	     response.setCharacterEncoding("UTF-8");
             	  //ajax请求
             	  if (request.getHeader("x-requested-with") != null && request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")){ //如果是ajax请求响应头会有x-requested-with  
                    System.err.println("ajax请求");
             		  PrintWriter out = response.getWriter();  
                    //   out.print("loseSession");//session失效
                       out.write("您没有此功能！");
                       out.flush();
                      
                   }else{

                 	      PrintWriter out=response.getWriter();
                 	     // String info ="<script>if(window.confirm('您没有权限')){window.history.back(0);}else{window.history.back(0);}</script>";	
                 	     String info="<script>alert('您没有权限访问');</script>";
                 	     out.write(info);
                      	     
                   }
        	}
        }else{       	
        	//没有绑定过滤的方法
        	super.doFilterInternal(request, response, filterChain);
        }
		}else{       	
        	//没有绑定过滤的方法
        	super.doFilterInternal(request, response, filterChain);
        }
	    
	
	} 
	
}

	
